<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
<!--        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>-->
        <!-- 过滤框 -->
        <el-select v-model="sightId" style="float:left" @change="changeSight" filterable placeholder="选择景区">
          <el-option v-for="(item, index) in sightOption" :key="index" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()" type="primary">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
<!--        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
        :data="dataList"
        border
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
      <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
      </el-table-column>
      <el-table-column
          prop="id"
          header-align="center"
          align="center"
          label="门票id">
      </el-table-column>
      <el-table-column
          prop="price"
          header-align="center"
          align="center"
          label="门票价格">
      </el-table-column>
      <el-table-column
          prop="sightName"
          header-align="center"
          align="center"
          label="景点名称">
      </el-table-column>
      <el-table-column
          prop="name"
          header-align="center"
          align="center"
          label="票种类">
      </el-table-column>
      <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './ticket-add-or-update'
export default {
  data () {
    return {
      sightOption: [],
      sightId: '',
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  mounted() {
    this.getDataList()
    this.getSightSelectList()
  },
  activated () {
    this.getDataList()
  },
  methods: {
    changeSight(value) {
      // 这里可以发送axios请求，将列表的信息转换为专属的信息，这里的value其实就是种类名称
      // console.log(value)
      // this.sightId = value
    },

    //获取景点下拉框
    getSightSelectList() {
      this.$http.get("/sights/list/select").then(res => {
        // console.log(res.data)
        this.sightOption = res.data.data;
      });
    },


    // 根据景点的关键字查询
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      if (this.value != '') {
        // 页面变动之后需要查询相同种类的变动页码的数据
        this.$http.get("/ticket/list/" + this.pageIndex + "/" + this.pageSize + "?sightId=" + this.sightId).then(res => {
          console.info(res.data.data)
          this.dataList = res.data.data.records;
          this.totalPage = res.data.data.total
          console.log(this.dataList)
        });
        return
      }
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      this.$http.delete("/ticket/delete/"+id).then(res => {
        this.$message({
          message: res.data.data,
          type: 'success'
        });
        this.getDataList()
      });
    }
  }
}
</script>
